import time

import pyautogui


def press(key):
    pyautogui.press(key)


def press_delay(key, delay):
    pyautogui.keyDown(key)
    time.sleep(delay)
    pyautogui.keyUp(key)


def mouse_left_click(loc):
    pyautogui.click(loc[0], loc[1])


def message(msg):
    pyautogui.typewrite(msg)


def screenshot(region=(0, 0, 1920, 1080)):
    # 截取整个屏幕并保存为文件
    screen = pyautogui.screenshot(region=region)
    # screen.save("../images/screenshot.png")
    return screen


def find_icon(icon_path, confidence=0.8, region=(0, 0, 1920, 1080)):
    try:
        return pyautogui.locateCenterOnScreen(image=icon_path,
                                              confidence=confidence,
                                              region=region,
                                              grayscale=True)
    except Exception as e:
        return None


def find_icon_offset(icon_path, offset=(0, 0), confidence=0.8, region=(0, 0, 1920, 1080)):
    try:
        _location = pyautogui.locateOnScreen(image=icon_path,
                                             confidence=confidence,
                                             region=region,
                                             grayscale=True)
        return (_location.x + offset[0], _location.y + offset[1])
    except Exception as e:
        return None


def slide_screen_up(loc=(500, 300), distance=50, delay=0.5):
    # 移动到起始位置
    pyautogui.moveTo(loc[0], loc[1] + distance)
    pyautogui.dragTo(loc[0], loc[1] - distance, button="left", duration=delay)


if __name__ == '__main__':
    slide_screen_up()
